package com.caiyigo.baihua.fetion;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import net.solosky.maplefetion.bean.Presence;

import org.apache.log4j.Logger;

public class fetion extends JFrame implements java.awt.event.ActionListener {
	private static final long serialVersionUID = -4884505908308759981L;
	private FetionClients demo;
	private static final int WIDTH = 800;
	private static final int HEIGHT = 600;
	private boolean zjFlag = false;
	public static StringBuffer helpMessage = new StringBuffer()//
			.append("\n" + "欢迎使用百花教育短信平台");

	JComboBox cbLicenceType = new JComboBox(new Object[] {//
			"请选择", //
					"登陆飞信"

			});

	JTextField tfMobile = new JTextField();
	JPasswordField tfPassword = new JPasswordField();
	JTextField pici = new JTextField();
	JTextField zJTextField = new JTextField();
	static JButton jbSendZW = new JButton("发送座位表");
	static JButton jbSendCJ = new JButton("发送成绩");
	static JButton jbSendZDY = new JButton("自定义");
	static JTextPane taInfo = new JTextPane();

	// JMenuItem exitMenu = new JMenuItem("退出");

	public fetion() throws IOException {
		super("百花教育短信平台 2013");
		initFrame();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setLocationRelativeTo(null);

	}

	public static void main(String[] args) throws IOException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException, UnsupportedLookAndFeelException,
			SQLException {
		UIManager
				.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		try {
			HibernateSessionFactory.getSession().beginTransaction();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "数据库未连接 ！");
		}
		new fetion();

	}

	public void initFrame() {
		initTopPanel();
		initCenterPanel();
	}

	public void initCenterPanel() {
		JScrollPane jsp = new JScrollPane(taInfo);
		jsp.setBounds(100, 90, 450, 300);
		getContentPane().add(jsp, "Center");
		taInfo.setText(helpMessage.toString());
	}

	/**
	 * 登陆
	 */
	public void login(String serviceid, String pasString) {
		demo = new FetionClients(serviceid, pasString);
		demo.login(Presence.ONLINE);

	}

	public void initTopPanel() {
		JPanel toppanel = new JPanel();
		toppanel.setLayout(null);
		JLabel lMobile = new JLabel("手机号码", JLabel.RIGHT);
		JLabel lPassword = new JLabel("密码", JLabel.RIGHT);
		JLabel lpici = new JLabel("批次号", JLabel.RIGHT);
		JLabel zJLabel = new JLabel("追加文字", JLabel.RIGHT);
		final JCheckBox jCheckBox = new JCheckBox("是");
		jCheckBox.setSelected(false);
		lMobile.setBounds(10, 10, 85, 30);
		tfMobile.setBounds(100, 10, 300, 30);
		cbLicenceType.setBounds(410, 10, 170, 30);
		lPassword.setBounds(10, 50, 85, 25);
		tfPassword.setBounds(100, 50, 300, 30);
		jbSendZDY.setBounds(410, 50, 100, 30);
		lpici.setBounds(10, 90, 85, 25);
		zJLabel.setBounds(10, 130, 85, 25);
		pici.setBounds(100, 90, 300, 30);
		zJTextField.setBounds(100, 130, 300, 30);
		jCheckBox.setBounds(410, 130, 300, 30);
		jbSendZW.setBounds(410, 87, 100, 30);
		jbSendCJ.setBounds(500, 87, 100, 30);

		jbSendZW.setEnabled(false);
		jbSendCJ.setEnabled(false);
		jbSendZDY.setEnabled(false);
		jbSendZW.addActionListener(this);
		jbSendCJ.addActionListener(this);
		jbSendZDY.addActionListener(this);
		java.awt.event.ActionListener listener = new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				JComboBox com = (JComboBox) e.getSource();
				String states = (String) com.getSelectedItem();
				if (states.equals("请选择")) {
				} else if (states.equals("登陆飞信")) {
					if ("".equals(tfMobile.getText())
							|| "".equals(tfPassword.getText())) {
						return;
					}
					login(tfMobile.getText(), tfPassword.getText());

				} else if (states.equals("退出飞信")) {
					try {

						jbSendCJ.setEnabled(false);
						jbSendZW.setEnabled(false);
						helpMessage.append("\n" + "成功退出!");
						taInfo.setText(fetion.helpMessage.toString());
					} catch (Exception e1) {

						e1.printStackTrace();
					}

				}

			}
		};
		jCheckBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == e.SELECTED) {
					zjFlag = true;
					zJTextField.setText("(本服务由百花教育提供,请考生自觉保留此考试信息.)");
				} else {
					zjFlag = false;
					zJTextField.setText("");
				}
			}

		});
		cbLicenceType.addActionListener(listener);
		toppanel.add(lMobile);
		toppanel.add(tfMobile);
		toppanel.add(cbLicenceType);
		toppanel.add(lPassword);
		toppanel.add(tfPassword);
		toppanel.add(lpici);
		toppanel.add(pici);
		toppanel.add(zJLabel);
		toppanel.add(zJTextField);
		toppanel.add(jCheckBox);
		toppanel.add(jbSendZW);
		toppanel.add(jbSendCJ);
		toppanel.add(jbSendZDY);
		JPanel tmpPanel = new JPanel();
		tmpPanel.add(toppanel, "Center");
		toppanel.setPreferredSize(new Dimension(600, 170));
		getContentPane().add(tmpPanel, "North");
		tfMobile.setText("15989203080");
		tfPassword.setText("cai7481063");
		zJTextField.setText("(本服务由百花教育提供,请考生自觉保留此信息.)");

	}

	public static String getCode(String fileName) {
		return (String) JOptionPane.showInputDialog(new Panel(), "输入验证码",
				"输入验证码", 1, new ImageIcon(fileName), null, null);

	}

	public void actionPerformed(java.awt.event.ActionEvent paramActionEvent) {
		Object source = paramActionEvent.getSource();
		String zj = zJTextField.getText();
		if (null != zj) {
			zj = zj.trim();
		} else {
			zj = "";
		}
		if (source == jbSendZW) {
			if (!StringUtils.validPici(pici.getText())) {
				JOptionPane.showMessageDialog(null, "批次错误");
				return;
			}
			try {
				if (zjFlag) {
					send(pici.getText().trim(), zj, StringUtils.TypeValues.ZW);
				} else {
					send(pici.getText().trim(), "", StringUtils.TypeValues.ZW);
				}

			} catch (HeadlessException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else if (source == jbSendCJ) {
			if (!StringUtils.validPici(pici.getText())) {
				JOptionPane.showMessageDialog(null, "批次错误");
				return;
			}
			try {
				if (zjFlag) {
					send(pici.getText().trim(), zj, StringUtils.TypeValues.CJ);

				} else {
					send(pici.getText().trim(), "", StringUtils.TypeValues.CJ);
				}

			} catch (HeadlessException e) {
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else if (source == jbSendZDY) {
			if (pici.getText() != null && !pici.getText().equals("8888")) {
				JOptionPane.showMessageDialog(null, "暗号错误 ！");
				return;
			}
			try {
				send(pici.getText().trim(), zj, StringUtils.TypeValues.ZDY);
			} catch (HeadlessException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (source.equals("退出")) {

		}
	}

	public void send(String pici, String appendMessage, String type)
			throws Exception {
		RunFetion m3 = new RunFetion(pici, appendMessage, type);
		Thread t1 = new Thread(m3);
		t1.start();
	}

	class RunFetion implements Runnable {

		String pici;
		String appendMessage;
		String type;

		RunFetion(String pici, String appendMessage, String type) {
			this.pici = pici;
			this.appendMessage = appendMessage;
			this.type = type;

		}

		public void run() {
			try {
				demo.tel(appendMessage, type, pici);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}

	}

}
